﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Services;

namespace AnesSystem.ViewModels
{
    public class AddAnesTemplateViewModel : BaseViewModel<AddAnesTemplateViewModel>
    {
        #region Variables
        private string _templateName;
        private bool _isGeren;



        #endregion


        #region Constructor
        public AddAnesTemplateViewModel()
        {
            InitData();
        }


        #endregion


        #region Properties
        public string TemplateName
        {
            get
            {
                if (string.IsNullOrEmpty(_templateName))
                {
                   _templateName=DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Nsss1;
                }
                return _templateName;
            }

            set
            {
                _templateName = value;
                RaisePropertyChanged("TemplateName");
            }
        }

        public bool IsGeren
        {
            get
            {
                return _isGeren;
            }

            set
            {
                _isGeren = value;
                RaisePropertyChanged("IsGeren");
            }
        }

        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private void InitData()
        {

        }


        public string AddTemplateModel()
        {
            if (string.IsNullOrEmpty(TemplateName))
            {
                MessageBoxCommon.ShowDialog("请输入模板名称！", MessageBoxCommon.OK);
                return Constants.FAILED;
            }
            AnesRecordTemplateModel template = new AnesRecordTemplateModel();
            if (IsGeren)
            {
                template.AccountId = DataManager.Instance.Login.Id.ToString();
            }
            else
            {
                template.AccountId = Constants.TEMPLATE_DEPARTMENT_ACCOUNTID;
            }
            template.CreateTime = DateTime.Now;
            List<PatientEventsModel> eventList = OperationDataPool.Instance.CurOperationData.EventDataCache.SearchAllEventExe();
            List<PatientDrugsModel> drugList = OperationDataPool.Instance.CurOperationData.DrugsDataCache.AllPatientDrugsList.ToList();
            CreatTemplate(eventList, drugList, template);
            template.Name = TemplateName.Trim();
            template.CreateTime = DateTime.Now;
            DataManager.Instance.AnesRecordTemplateModelList.Add(template);
            AnesTemplateCtrlViewModel.Instance.Refresh();
            AnesRecordTemplateModelService.Instance.AddUpdate(template);
            return Constants.SUCCESS;

        }

        public void CreatTemplate(List<PatientEventsModel> eventList, List<PatientDrugsModel> drugList, AnesRecordTemplateModel template)
        {
            List<DateAndName> drugAndEventList = new List<DateAndName>();
            AddEvents(drugAndEventList, eventList);
            AddDrugs(drugAndEventList, drugList);
            SortByTime(drugAndEventList);
            List<ItemInfoModel> itemInfos = new List<ItemInfoModel>();
            foreach (var item in drugAndEventList)
            {
                ItemInfoModel itemInfo = new ItemInfoModel();
                if (item.IsEvent)
                {
                    EventExeInfoModel eventExeInfo = new EventExeInfoModel();
                    if (string.IsNullOrEmpty(item.Name))
                    {
                        continue;
                    }
                    eventExeInfo.EventCode = item.Name;
                    eventExeInfo.IsEnd = item.IsEnd;
                    itemInfo.EventExeInfo = eventExeInfo;
                }
                else
                {
                    DrugExeInfoModel drugInfo = new DrugExeInfoModel();
                    drugInfo.IsOnce = item.IsOnce;
                    drugInfo.Type = item.Type;
                    drugInfo.Drugs = item.Drugs;
                    drugInfo.DrugMethod = item.DrugMethod;
                    drugInfo.DrugReason = item.DrugReason;
                    drugInfo.Channel = item.Channel;
                    drugInfo.LiquidType = item.LiquidType;
                    drugInfo.DrugSpeeds = item.DrugSpeeds;
                    itemInfo.DrugExeInfo = drugInfo;
                }
                itemInfos.Add(itemInfo);
            }
            template.ItemInfoList = itemInfos;

        }

        private void SortByTime(List<DateAndName> drugAndEventList)
        {
            drugAndEventList.Sort((DateAndName o1, DateAndName o2) =>
            {
                if (o1.Date < o2.Date)
                {
                    return -1;
                }
                else if (o1.Date > o2.Date)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            });
        }

        private void AddDrugs(List<DateAndName> drugAndEventList, List<PatientDrugsModel> drugList)
        {
            foreach (var item in drugList)
            {
                DateAndName dateAndName = new DateAndName();
                dateAndName.Date = item.BeginTime;
                dateAndName.Name = "";
                dateAndName.Drugs = item.Drugs;
                dateAndName.DrugMethod = item.DrugMethod;
                dateAndName.Channel = item.Chanel;
                dateAndName.DrugReason = item.DrugReason;
                dateAndName.LiquidType = item.LiquidType;
                dateAndName.DrugSpeeds = item.DrugSpeeds;
                if (item.Single)
                {
                    dateAndName.IsOnce = true;
                }
                dateAndName.Type = item.Type;
                drugAndEventList.Add(dateAndName);
            }
        }

        private void AddEvents(List<DateAndName> drugAndEventList, List<PatientEventsModel> eventList)
        {
            foreach (var item in eventList)
            {
                string code = item.Code;
                if (string.IsNullOrEmpty(code) || Constants.EVENT_IN_ROOM.Equals(code))
                {
                    continue;
                }
                ConfigEventsModel cem = null;
                if (DataManager.Instance.Code2events.ContainsKey(code))
                {
                    cem = DataManager.Instance.Code2events[code];
                }
                if (cem == null)
                {
                    Common.LogHelper.Info(this.GetType(), "not find eventCode=" + code + ",When add AnesTemplate");
                    continue;
                }
                bool isSingle = cem.Single;
                DateAndName dateAndName = new DateAndName();
                dateAndName.Date = item.BeginTime;
                dateAndName.IsEvent = true;
                dateAndName.Name = item.Code;
                drugAndEventList.Add(dateAndName);
                if (!isSingle)
                {
                    DateTime endTime = item.EndTime;
                    if (!endTime.Equals(new DateTime()))
                    {
                        DateAndName dateAndName1 = new DateAndName();
                        dateAndName1.Date = endTime;
                        dateAndName1.IsEnd = true;
                        dateAndName1.Name = item.Code;
                        dateAndName1.IsEvent = true;
                        drugAndEventList.Add(dateAndName1);
                    }
                }

            }
        }



        #endregion


        #region Public/ProtectedMethods


        #endregion


        #region EventHandlers


        #endregion
    }

    class DateAndName
    {
        /// <summary>
        /// 时间索引
        /// </summary>
        private DateTime date;
        /// <summary>
        /// 事件code或药ID
        /// </summary>
        private string name;
        private DrugsModel drugs;
        private bool isEvent;
        private string type;
        private bool isOnce;
        private string channel;
        private string drugMethod;
        private bool isEnd;
        private string drugReason;
        private List<DrugSpeedModel> drugSpeeds;
        private string liquidType;

        public DateTime Date
        {
            get
            {
                return date;
            }

            set
            {
                date = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public DrugsModel Drugs
        {
            get
            {
                return drugs;
            }

            set
            {
                drugs = value;
            }
        }

        public bool IsEvent
        {
            get
            {
                return isEvent;
            }

            set
            {
                isEvent = value;
            }
        }

        public string Type
        {
            get
            {
                return type;
            }

            set
            {
                type = value;
            }
        }

        public bool IsOnce
        {
            get
            {
                return isOnce;
            }

            set
            {
                isOnce = value;
            }
        }

        public string Channel
        {
            get
            {
                return channel;
            }

            set
            {
                channel = value;
            }
        }

        public string DrugMethod
        {
            get
            {
                return drugMethod;
            }

            set
            {
                drugMethod = value;
            }
        }

        public bool IsEnd
        {
            get
            {
                return isEnd;
            }

            set
            {
                isEnd = value;
            }
        }

        public string DrugReason
        {
            get
            {
                return drugReason;
            }

            set
            {
                drugReason = value;
            }
        }


        public List<DrugSpeedModel> DrugSpeeds
        {
            get
            {
                return drugSpeeds;
            }

            set
            {
                drugSpeeds = value;
            }
        }

        public string LiquidType
        {
            get
            {
                return liquidType;
            }

            set
            {
                liquidType = value;
            }
        }
    }
}
